package com.javaee.term.service.iml;

import com.javaee.term.dto.ContactDetailDto;
import com.javaee.term.dto.ProductDto;
import com.javaee.term.model.Contact;
import com.javaee.term.model.ContactDetail;
import com.javaee.term.model.Product;
import com.javaee.term.model.SendProductList;
import com.javaee.term.service.ContactDetailService;
import com.javaee.term.service.ContactService;
import com.javaee.term.service.ProductService;
import com.javaee.term.service.SendProductService;
import com.javaee.term.service.base.BaseServiceIml;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * author 陈伯瑞
 * date 2019/12/16 0016 19:39
 * version 1.0
 **/
@Service
public class ContactDetailServiceIml extends BaseServiceIml implements ContactDetailService {
    @Autowired
    ContactService  contactService;
    @Autowired
    ProductService  productService;
    @Autowired
    SendProductService sendProductService;

    @Override
    public ContactDetailDto findContactDetail(String contactId) {
        List<ContactDetail> contactDetails=contactDetailMapper.selectByContactId(contactId) ;
        Contact contact=contactService.findOne(contactId);

        //合同详情封装到dto
        ContactDetailDto contactDetailDto=new ContactDetailDto();
        BeanUtils.copyProperties(contact,contactDetailDto);
        List<ProductDto> productDtos = new ArrayList<>();
        for(ContactDetail contactDetail:contactDetails) {
            Product product=productService.findOne(contactDetail.getProductId());
            ProductDto productDto=new ProductDto();
            BeanUtils.copyProperties(product, productDto);
            productDto.setNumber(contactDetail.getProductQuantity());
            productDto.setPrintstatus(contactDetail.getPrintStatus());
           //合同对应商品发货单缺少的数量
            List<SendProductList> sendProductLists= sendProductService.findByContactIdAndProductId(contactId,contactDetail.getProductId());
            Integer out = sendProductLists.stream().mapToInt(SendProductList::getProductQuantity).sum();
            Integer notout=contactDetail.getProductQuantity()-out;
            productDto.setNotout(notout);
            productDto.setProductamount(product.getProductPrice().multiply(BigDecimal.valueOf(contactDetail.getProductQuantity())));
            productDtos.add(productDto);
        }
        contactDetailDto.setProductDtos(productDtos);
        return contactDetailDto;
    }

    @Override
    public List<ContactDetail> findDetails(String contactId) {
        return contactDetailMapper.selectByContactId(contactId);
    }

    @Override
    public ContactDetail findByContactProduct(String contactId, String productId) {
        return contactDetailMapper.selectByContactProduct(contactId,productId);
    }


    @Override
    public ContactDetail findOne(Object id) {
        return contactDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<ContactDetail> findAll() {
        return contactDetailMapper.selectAll();
    }

    @Override
    public void delete(Object id) {
        contactDetailMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(ContactDetail contactDetail) {
        return contactDetailMapper.insert(contactDetail);
    }

    @Override
    public int update(ContactDetail contactDetail) {
        return contactDetailMapper.updateByPrimaryKey(contactDetail);
    }
}
